package cn.bx.bid.ctrl;

import cn.bx.bid.entity.AjaxResult;
import cn.bx.bid.entity.User;
import cn.bx.bid.entity.Userrole;
import cn.bx.bid.service.RoleService;
import cn.bx.bid.service.UserService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

//用户登录

    @RequestMapping("login1")
    public AjaxResult login1(HttpServletRequest request, @RequestParam("username") String username, @RequestParam("password") String pass) {
        User u = userService.login(username, pass);
        if (u != null) {
            return AjaxResult.success("true", u);
        }

        return AjaxResult.error("false");
    }

    //用户查询列表
    @GetMapping(value = "userSearch",produces = "application/json;charset=utf-8")
    public cn.bx.bid.entity.Page<User> search(@RequestParam(value = "page",defaultValue = "1") int pageNo, @RequestParam(value = "limit",defaultValue = "10") int limit, @RequestParam(value = "name",required = false) String name, @RequestParam(value = "startDate",required = false) Date start, @RequestParam(value = "endDate",required = false) Date end){
        pageNo--;
        Page<User> p=userService.paging(pageNo, limit, name, start, end);
        cn.bx.bid.entity.Page<User> userPage = cn.bx.bid.entity.Page.toPage(p);
        return userPage;
    }

    //查询单个用户信息
    @RequestMapping("touser-look-{id}")
    public ModelAndView userLook(@PathVariable("id") long id){
        User user=  userService.get(id);
        System.out.println("1>>>>>>>>>>>>>"+user.getEnterdate());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String date=  simpleDateFormat.format(user.getEnterdate());
        System.out.println("2>>>>>>>>>>>>>"+date);
        ModelAndView mv=new ModelAndView();
        mv.addObject("user",user);
        mv.setViewName("userupdate");
        return mv;
    }

    //查询修改用户信息
    @GetMapping(value = "userGet",produces = "application/json;charset=utf-8")
    public  User userGet(@RequestParam("id")long id){
        User u=  userService.get(id);
        return u;

    }

    //用户注册
    @GetMapping(value = "userAdd",produces = "application/json;charset=utf-8")
    public  AjaxResult userAdd(@ModelAttribute("u") User user){
        //   UserService userService=SpringUtil.getBean(UserService.class);
        int a= userService.save(user);
        if(a>0){
            System.out.println("添加成功");
            return AjaxResult.success("true");
        }
        return AjaxResult.error("false");
    }

    @RequestMapping(value = "findRoles", produces = "application/json;charset=utf-8")
    public AjaxResult findRoles() {
        List<Userrole> li = roleService.findAll();
        System.out.println(li);
        return AjaxResult.success(li);
    }

    //更新用户信息
    @RequestMapping(value = "userUpdate",produces = "application/json;utf-8")
    public AjaxResult userUpdate(@ModelAttribute("u")User user){
        int a=userService.update(user);

        if(a>0){
            return AjaxResult.success("true");
        }
        return  AjaxResult.error("false");
    }

    //删除用户
    @RequestMapping("userdel")
    public AjaxResult del1(HttpServletRequest request, @RequestParam("id") long id){
        int u= userService.delBy(id);
        if(u>0){
            return AjaxResult.success("删除成功");
        }
        return  AjaxResult.error("删除失败");
    }

    @RequestMapping(value = "checkUserNameRepeat",produces = "application/json;charset=utf-8")
    public AjaxResult checkUserNameRepeat(@RequestParam("userName")String userName, @RequestParam(value = "id",required = false)Long id){
        boolean a= userService.checkUserNameRepeat(id,userName);
        if(a){
            return  AjaxResult.error("登录名重复");
        }
        return AjaxResult.success("登录名不存在");
    }

    /*@RequestMapping(value = "userUpdate2"  ,produces = "application/json;charset=utf-8")
    public AjaxResult userUpdate2(@ModelAttribute("user")User user){
        int r = this.userService.update(user);
        if (r == 0){
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.success("修改成功");
    }*/

    @RequestMapping(value = "batchDeluser",produces = "application/json;charset=utf-8")
    public AjaxResult batchDel(@RequestParam("ids[]")long[] ids){
        int b=  userService.batchDel(ids);
        return b>0?AjaxResult.success("删除成功"):AjaxResult.error("删除失败");
    }

    @RequestMapping(value = "updatePass",produces = "application/json;utf-8")
    public AjaxResult updatePass(@ModelAttribute("u")User user){
        int a=userService.updatePass(user);

        if(a>0){
            return AjaxResult.success("true");
        }
        return  AjaxResult.error("false");
    }

    //异常处理
    @ExceptionHandler
    public  AjaxResult error(Exception e){
        return  AjaxResult.error("false",e);
    }
    //设置日期格式
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值
    }
}
